package com.htwy.core.controller;

import com.htwy.core.model.Dictionary;
import com.htwy.ext.BaseController;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Okv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;

public class DictionaryController extends BaseController<Dictionary> {

    @Override
    protected Dictionary dao() {
        return Dictionary.dao;
    }

    public void deleteCascade(){
        String pk = getPara("uuid");
        renderJson(Ret.ok().set("data",dao().deleteByIdCascade(pk)));
    }

    public void findByParentCodes(){
        String[] codes = getParaValues("codes");
        List<Record> dicts = dao().findByParentCodes(codes);
        if(dicts != null) {
            Kv data = Kv.create();
            for (Record temp : dicts) {
                String code = temp.getStr("parentCode");
                if (data.containsKey(code)) {
                    Okv dictKv = data.getAs(code);
                    dictKv.set(temp.getStr("code"), temp.getStr("label"));
                } else {
                    Okv dictKv = Okv.by(temp.getStr("code"), temp.getStr("label"));
                    data.set(temp.getStr("parentCode"),dictKv);
                }
            }
            renderJson(Ret.ok().set("data",data));
        }else{
            renderJson(Ret.fail().set("msg","至少需要一个分类"));
        }
    }

}